require "Common/define"
require "Logic/Config"
require "Common/functions"
Btn_DrowProbablityItem = {}
HelperFunc.SetUIEnv(Btn_DrowProbablityItem)
local this = Btn_DrowProbablityItem;

this.tableUpContent = {}
this.qualityQualityContent={}
this.typeController=nil;
this.dynamicList =nil;

function Btn_DrowProbablityItem.initData(obj,data)
    this.typeController = obj:GetController("Type")
   local isUp= data["isUp"];
   local isTip = data["isTip"]
   if isUp then
    this.dynamicList = obj:GetChild("List_UpHero");
    this.typeController:SetSelectedIndex(0)
    local content = data["content"]
    local upPoolText = obj:GetChild("Text_UpPool");
    upPoolText.text = getLanguage("Draw_Detail_Up");
    this.tableUpContent={}
    local content = data["content"];
    if content ~= nil then
        for i = 1, #content do
            local v={}
            v.heroId=content[i][1]
            v.drawRate = content[i][2]
            if logicMgr.HeroManager.isHeroFullyOpen(v.heroId) then
                table.insert(this.tableUpContent, v);
            end
        end
    end
   if content ~= nil and  #content>0 then
    this.showContent(true);
   end
   

    else if isTip then
        this.typeController:SetSelectedIndex(5)
        local tipsSign =obj:GetChild("Text_Text");
        local tipsText = obj:GetChild("Text_Tips");
        tipsSign.text =getLanguage("Draw_Detail_Des");
        tipsText.text =data["content"];
    else
        this.dynamicList = obj:GetChild("List_HeroList");
        local quality = data["quality"]
        local index = quality
        this.typeController:SetSelectedIndex(index)
        local textSign =obj:GetChild("Text_HeroList");
        textSign.text =getLanguage("Draw_Detail_List");
       
    
        local probabilityText = obj:GetChild("Text_HeroListProbability")
        probabilityText.text = getLanguage("Sys_Chance") .. (data["probability"] / 100) .. "%"
     
        this.qualityQualityContent={}
        local content = data["content"];
        if content ~= nil then
            for i = 1, #content do
                local v={}
                v.heroId=content[i]
                if logicMgr.HeroManager.isHeroFullyOpen(v.heroId) then
                    table.insert(this.qualityQualityContent, v);
                end
            end
        end
       
        this.showContent(false);
    end
   end

  
end


function Btn_DrowProbablityItem.showContent(isUp)
    if isUp then
        dynamicList.itemRenderer=this.setUpData
        dynamicList.numItems = #this.tableUpContent
        this.dynamicList.height =420* #this.tableUpContent/4+1
    else
        dynamicList.itemRenderer=this.setData
        dynamicList.numItems = #this.qualityQualityContent
        this.dynamicList.height = 220* math.ceil(#this.qualityQualityContent/4) + 45
    end
    -- HelperFunc.ListTranstionPlay(dynamicList)
    -- dynamicList:ScrollToView(0);
end

    function Btn_DrowProbablityItem.setUpData(index,itemObj)
        local itemVal = this.tableUpContent[index+1];
        local go = itemObj
        local controller = go:GetController("probability");
        controller:SetSelectedIndex(1)
        local drawRate = go:GetChild("drawRate")
        local name = go:GetChild("name")
        local hero = go:GetChild("hero")

        local curHeroData = logicMgr.HeroManager.getHeroDatabyModelId(itemVal["heroId"],true);
        drawRate.text = getLanguage("Sys_Chance") .. (itemVal["drawRate"] / 100) .. "%"
        name.text = curHeroData.name;
        local ctrlItem = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
        if ctrlItem ~= nil then
            local data = {};
            data.star = logicMgr.HeroManager.starToShow(logicMgr.HeroManager.GetHeroStarByModelId(itemVal["heroId"]));
            data.icon = curHeroData.icon;
            data.quality = logicMgr.HeroManager.GetHeroQualityByModelId(itemVal["heroId"]);
            data.type = curHeroData.type;
            ctrlItem.initData(hero, data);
            hero.onClick:Clear();
        end

        go.onClick:Clear();
        go.onClick:Add(function()
            local heros = {}
            table.insert(heros, logicMgr.HeroManager.getHeroDatabyModelId(itemVal["heroId"]))
            showUI(PanelNames.UICharactorPreview, heros, itemVal["heroId"], true);
        end)
    end

    function Btn_DrowProbablityItem.setData(index,itemObj)
        local itemVal = this.qualityQualityContent[index+1];
        local go = itemObj
        local controller = go:GetController("probability");
        controller:SetSelectedIndex(0)

        local name = go:GetChild("name")
        local hero = go:GetChild("hero")
        local curHeroData = logicMgr.HeroManager.getHeroDatabyModelId(itemVal["heroId"],true);
        local curHeroConfig = logicMgr.HeroManager.GetHeroConfigbyModelId(itemVal["heroId"])
        name.text = string.TextColor(getLanguage(curHeroConfig.f_HeroName), Config.ColorValue[logicMgr.HeroManager.starToQuality(curHeroConfig.f_HeroInitialStar)]);
        local ctrlItem = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
        if ctrlItem ~= nil then
            local data = {};
            data.star = logicMgr.HeroManager.starToShow(logicMgr.HeroManager.GetHeroStarByModelId(itemVal["heroId"]));
            data.icon = curHeroData.icon;
            data.quality = logicMgr.HeroManager.GetHeroQualityByModelId(itemVal["heroId"]);
            data.type = curHeroData.type;
            data.race = curHeroConfig.f_HeroRace
            data.faction = curHeroConfig.f_FactionType
            ctrlItem.initData(hero, data);
            hero.onClick:Clear();
        end

        go.onClick:Clear();
        go.onClick:Add(function()
            local heros = {}
            table.insert(heros, logicMgr.HeroManager.getHeroDatabyModelId(itemVal["heroId"]))
            showUI(PanelNames.UICharactorPreview, heros, itemVal["heroId"], true);
        end)
    end

return Btn_DrowProbablityItem;
